$(document).ready(function(){
	$("#searchbtn").click(function(){
		var keyword = $("#keywordInput").val();
		console.log(keyword);
		keyword = encodeURI(keyword);
		console.log(keyword);
		$.ajax({
			"type":"get",
			"url":"../../CorrelationInfoServlet?method=correlationInfo",
			"data":{
				"wd":keyword,
			},
			success:function(data){
				var keywordList =JSON.parse(data).correlationWords;
				console.log(keywordList);
//				var word_list ="";
//				/*$.each(keywordList,function(index,value){
//					var ele="{text:"+this.keyword+", weight:"+this.weight+", link:{href:\"../jsp/sentimentList.jsp?keyword="+this.keyword+"\", target:\"_blank\"}},";
//					word_list=word_list+ele;
//				});*/
//				for(var i=0;i<keywordList.length-1;i++){
//					//var ele="{text:"+ "\"" +keywordList[i].keyword + "\"" +", weight:"+keywordList[i].weight+", link:{href:\"../jsp/sentimentList.jsp?keyword="+keywordList[i].keyword+"\", target:\"_blank\"}},";
//					var ele="{text:"+ "\"" +keywordList[i].keyword + "\"" +", weight:"+keywordList[i].weight+"},";
//					word_list=word_list+ele;
//				}
//				//var elelast = "{text:" + "\"" +keywordList[keywordList.length-1].keyword + "\"" +", weight:"+keywordList[keywordList.length-1].weight+", link:{href:\"../jsp/sentimentList.jsp?keyword="+keywordList[keywordList.length-1].keyword+"\", target:\"_blank\"}}";
//				var elelast = "{text:" + "\"" +keywordList[keywordList.length-1].keyword + "\"" +", weight:"+keywordList[keywordList.length-1].weight+"}";
//				alert(word_list);
//				word_list=word_list+elelast;
//				word_list="["+word_list+"]";
				
				var test=new Array();

				for(var i=0;i<keywordList.length;i++){
					
					test[i]={};
					test[i].text=keywordList[i].word;
					test[i].weight=keywordList[i].weight;
					test[i].link={};
					test[i].link.href="../jsp/sentimentList.jsp?keyword="+keywordList[i].word;
					test[i].link.target="\"_blank\"";
					
					//alert(test[i].text+":"+test[i].weight+":"+test[i].link.href+":"+test[i].link.target);
				}
//				重复显示问题未解决
				if($("#ResultDIV").is(":hidden")){
					createjQCloud(test);
				}
				else{
//					清除原先div内容
					$('#my_favorite_latin_words').empty();
					createjQCloud(test);
				}
				
//				createjQCloud(test);

			},
			error:function(){
				console.log("error");
			}
		})
	});
	
})

function createjQCloud(word_list){
	
					$("#my_favorite_latin_words").jQCloud(word_list,{
						delayedMode:true,
						width:1300,
						height:250
					});
					
					 $('#ResultDIV').show('slow');
				};